﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MuzičkiFeršterker
{
    public partial class PlayList
    {
        /// <summary>
        /// Method used to export current playlist to XSPF format
        /// </summary>
        /// <param name="filepath">Path with location of desired saved playlist</param>
        public void ExportToXSPF(string filepath)
        {
            StreamWriter write = new StreamWriter(filepath);
            write.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            write.WriteLine("<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">");
            write.WriteLine("   <trackList>");
            foreach (Song a in filesList)
            {
                write.WriteLine("      <track><location>file:///" + a.Path.Replace("\\", "/").Replace(" ", "%20") + "</location></track>");
            }
            write.WriteLine("   </trackList>");
            write.WriteLine("</playlist>");
            write.Close();
        }

        /// <summary>
        /// Loads a XSPF playlist and appends it to current playlist
        /// </summary>
        /// <param name="filepath">Path to the XSPF formated playlist</param>
        public void ImportFromXSPF(string filepath)
        {
            if (File.Exists(filepath))
            {
                StreamReader read = new StreamReader(filepath);
                PatternSearcher ps = new PatternSearcher("<location>", "</location>");
                Uri b;
                foreach(string a in ps.AllMatches(read.ReadToEnd(), true))
                {
                    b = new Uri(a);
                    this.Add(b.AbsolutePath.Replace("%20", " "));
                }
                read.Close();
            }
        }
    }
}
